import requests
from lxml import etree
import os
if __name__ == "__main__":
    # url = 'https://www.aqistudy.cn/historydata/'
    # headers={
    #     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67'
    # }
    # page_text=requests.get(url=url,headers=headers).text

    # tree = etree.HTML(page_text)
    # hot_li_list = tree.xpath('//div[@class="bottom"]/ul/li')
    # all_city_names=[]
    # #解析到了热门城市
    # for li in hot_li_list:
    #     hot_city_name = li.xpath('./a/text()')[0]
    #     all_city_names.append(hot_city_name)
    
    # city_names = tree.xpath('//div[@class="bottom"]/ul/div[2]/li')
    # for li in city_names:
    #     city_name = li.xpath('./a/text()')[0]
    #     all_city_names.append(city_name)   

    # print(all_city_names)  
    # print(len(all_city_names))  
    url = 'https://www.aqistudy.cn/historydata/'
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67'
    }
    page_text=requests.get(url=url,headers=headers).text

    tree = etree.HTML(page_text)
    #解析到热门城市和所有城市对应的a标签 
    # div/ul/li/a           热门城市a标签层级关系
    # //div/ul/div[2]/li/a    全部城市a标签层级关系
    all_city_names=[]
    a_list = tree.xpath('//div[@class="bottom"]/ul/li/a | //div[@class="bottom"]/ul/div[2]/li/a')
    for a in a_list:
        city_name = a.xpath('./text()')[0]
        all_city_names.append(city_name)
    
    print(all_city_names,len(all_city_names))